home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Images and Bitmaps / Bounce / Bounce.cs next >
Encoding:
Text File  |  2001-01-15  |  2.7 KB  |  92 lines

  1. //-------------------------------------
  2. // Bounce.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class Bounce: Form
  9. {
  10.      const  int iTimerInterval = 25;    // In milliseconds
  11.      const  int iBallSize = 16;         // As fraction of client area
  12.      const  int iMoveSize = 4;          // As fraction of ball size
  13.  
  14.      Bitmap bitmap;
  15.      int    xCenter, yCenter;
  16.      int    cxRadius, cyRadius, cxMove, cyMove, cxTotal, cyTotal;
  17.  
  18.      public static void Main()
  19.      {
  20.           Application.Run(new Bounce());
  21.      }
  22.      public Bounce()
  23.      {
  24.           Text = "Bounce";
  25.           ResizeRedraw = true;
  26.           BackColor = Color.White;
  27.  
  28.           Timer timer = new Timer();
  29.           timer.Interval = iTimerInterval;
  30.           timer.Tick += new EventHandler(TimerOnTick);
  31.           timer.Start();
  32.  
  33.           OnResize(EventArgs.Empty);
  34.      }
  35.      protected override void OnResize(EventArgs ea)
  36.      {
  37.           Graphics grfx = CreateGraphics();
  38.           grfx.Clear(BackColor);
  39.  
  40.           float fRadius = Math.Min(ClientSize.Width  / grfx.DpiX,
  41.                                    ClientSize.Height / grfx.DpiY) 
  42.                                         / iBallSize;
  43.  
  44.           cxRadius = (int) (fRadius * grfx.DpiX);
  45.           cyRadius = (int) (fRadius * grfx.DpiY);
  46.  
  47.           grfx.Dispose();
  48.                
  49.           cxMove = Math.Max(1, cxRadius / iMoveSize);
  50.           cyMove = Math.Max(1, cyRadius / iMoveSize);
  51.  
  52.           cxTotal = 2 * (cxRadius + cxMove);
  53.           cyTotal = 2 * (cyRadius + cyMove);
  54.  
  55.           bitmap = new Bitmap(cxTotal, cyTotal);
  56.  
  57.           grfx = Graphics.FromImage(bitmap);
  58.           grfx.Clear(BackColor);
  59.  
  60.           DrawBall(grfx, new Rectangle(cxMove, cyMove, 
  61.                                        2 * cxRadius, 2 * cyRadius));
  62.           grfx.Dispose();
  63.  
  64.           xCenter = ClientSize.Width / 2;
  65.           yCenter = ClientSize.Height / 2;
  66.      }
  67.      protected virtual void DrawBall(Graphics grfx, Rectangle rect)
  68.      {
  69.           grfx.FillEllipse(Brushes.Red, rect);
  70.      }
  71.      void TimerOnTick(object obj, EventArgs ea)
  72.      {
  73.           Graphics grfx = CreateGraphics();
  74.  
  75.           grfx.DrawImage(bitmap, xCenter - cxTotal / 2,
  76.                          yCenter - cyTotal / 2,
  77.                          cxTotal, cyTotal);
  78.           grfx.Dispose();
  79.  
  80.           xCenter += cxMove;
  81.           yCenter += cyMove;
  82.  
  83.           if ((xCenter + cxRadius >= ClientSize.Width) || 
  84.               (xCenter - cxRadius <= 0))
  85.                     cxMove = -cxMove;
  86.  
  87.           if ((yCenter + cyRadius >= ClientSize.Height) || 
  88.               (yCenter - cyRadius <= 0))
  89.                     cyMove = -cyMove;
  90.      }
  91. }
  92.